require(["config"],function(){
	require(["jquery","template","header","footer","fdj","cookie"],function($,template,header,footer){
		$("footer").load("/html/component/footer.html",function(){})
		$("header").load("/html/component/header.html",function(){
			header.news().init().xiding().welcome();
			var ss = location.search.slice(1),
				arr = ss.split("="),
				obj = {};
			obj[arr[0]] = arr[1];
			$.ajax({
				type:"get",
				data:obj,
				url:"http://rap2api.taobao.org/app/mock/117569/xqy2",
				success: function(res){
					var html = template("pro-template",{pro: res});
					$(".content").html(html);
					var obj1 = {
								picbox:"picbox",
								fdj:"fdj",
								bigbox:"bigbox",
								bigpic:"bigpic"
							}
					$(".content").fdj(obj1);
					
					//购物车cookie操作
					var size = $(".informt ul li").eq(0).html();
					var obj1 = {};
					var arr = [];
					obj1.id = obj.id;
					obj1.img1 = res.img1;
					obj1.price = res.price;
					obj1.listname = res.listname;
					obj1.proname = res.proname;
					obj1.size = size;
					console.log(obj1)
					
					//默认尺码样式加在第一个li上
					var size = $(".informt ul li").eq(0).html();
					$(".informt ul li").eq(0).addClass("ac");
					$(".informt ul li").click(function(){
						$(this).addClass("ac").siblings().removeClass("ac");
						size = $(this).html();
						obj1.size = size;
					})
					
					//点击购物车存商品信息,抛物线效果
					var btn = $("#add"),
						gwc = $(".span2");
					var p2 = {
							x: gwc.offset().left,
							y: gwc.offset().top
						};
					/*$(document).scroll(function(){
						p2.y = 0;
					})*/
					$("#add").click(function(obj2){
						
							
						var p1 = {
							x: btn.offset().left,
							y: btn.offset().top
						};
						//终点
						
						//顶点
						//top的纵坐标不能超出屏幕
						var p3 = {
							x: p2.x - 200,
							y: p2.y - 30 > 0 ? p2.y - 50 : 0
						};
				
						var a = -((p2.y-p3.y)*p1.x-(p2.x-p3.x)*p1.y+p2.x*p3.y-p3.x*p2.y)/((p2.x-p3.x)*(p1.x-p2.x)*(p1.x-p3.x));
						var b = ((p2.y-p3.y)*p1.x*p1.x+p2.x*p2.x*p3.y-p3.x*p3.x*p2.y-(p2.x*p2.x-p3.x*p3.x)*p1.y)/((p2.x-p3.x)*(p1.x-p2.x)*(p1.x-p3.x));
						var c = ((p2.x*p3.y-p3.x*p2.y)*p1.x*p1.x-(p2.x*p2.x*p3.y-p3.x*p3.x*p2.y)*p1.x+(p2.x*p2.x*p3.x-p2.x*p3.x*p3.x)*p1.y)/((p2.x-p3.x)*(p1.x-p2.x)*(p1.x-p3.x));
						
						//创建元素让他进行抛物线运动
						var div =$("<div>");
						div.css({
		                        "width":"20",
		                        "height":"20",
		                        "background":"skyblue",
		                        "position":"absolute",
		                        "left":p1.x,
		                        "top": p1.y,
		                        "z-index":"100"
		                        }).appendTo("body");
		
						var _left = p1.x;
						var _top;
						//横坐标水平右移，纵坐标按照抛物线计算
						var timer = setInterval(function(){
							_left+=18;
							_top = a*_left*_left + b*_left + c;
							div.css({"left":_left,"top":_top})
							/*div.style.left = _left + "px";
							div.style.top = _top + "px";*/
							if(_left >= p2.x){
								//加入购物车成功，到达终点
								clearInterval(timer);
								div.remove();
							}
						},30);	

						if (!$.cookie("shopping")) {
							obj1.num = $(".num").val();
							arr.push(obj1);
							var json = JSON.stringify(arr);
							$.cookie("shopping",json,{path: '/'});
						} else{
							var str = JSON.parse($.cookie("shopping"));
							obj1.num = $(".num").val();
							for (var i=0;i<str.length;i++) {
						 		if (str[i].id === obj1.id) {
						 	    str[i].num =str[i].num*1 + obj1.num*1;
						 	    break;
					    		}
						 	}
							if (str.length === i) {
								obj1.num = $(".num").val();
						 		str.push(obj1);
						 	}
							var str1 = JSON.stringify(str);
							$.cookie("shopping",str1,{path: '/',expires:7});
							console.log(str1)
						}
					})
				}
			})
		})
	})
})
